<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "tbl_upfile".
 *
 * @property string $id
 * @property string $url
 * @property string $file
 * @property string $uid
 */
class Upfile extends \yii\db\ActiveRecord {

    /**
     * 添加文件上传记录
     */
    public static function addjilu($file, $url,$md5) {
        /// var_dump($file,$url);exit;
        $new = new Upfile();
        $new->file = $file;
        $new->url = $url;
        $new->time = time();
        $new->md5 =$md5;
        $new->uid = Yii::$app->user->id;
        $new->save();
    }

    /**
     * 查询数据库,如果有已经上传的,返回
    */
    public static function getUrl($md5){
        $one=self::find()->where([
            'md5'=>$md5
        ])->select(['md5','url','file'])->limit(1)->one();
        if(!$one){
            return false;
        }else{
            return [
                'img'=>$one->url,
                'file'=>$one->file
            ];
        }
    }









    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'tbl_upfile';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['url', 'file'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'url' => '访问地址',
            'file' => '物理地址',
            'time' => '时间',
            'uid' => '用户id'
        ];
    }

}
